function convert(input) { alert('Error. Contact us if this keeps happening.'); document.getElementById('modal-contact').classList.add('show'); } function _dbPad(n) { return n < 10 ? '0' + n : String(n); } function _dbIsoToday() { var d = new Date(); return d.getFullYear() + '-' + _dbPad(d.getMonth() + 1) + '-' + _dbPad(d.getDate()); } function _dbParse(s) { if (!s) return null; var m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s); if (!m) return null; var y = +m[1], mo = +m[2] - 1, dd = +m[3]; var d = new Date(Date.UTC(y, mo, dd)); if (d.getUTCFullYear() !== y || d.getUTCMonth() !== mo || d.getUTCDate() !== dd) return null; return d; } function _dbDiffYearsMonthsDays(a, b) { var start = a < b ? a : b; var end = a < b ? b : a; var years = end.getUTCFullYear() - start.getUTCFullYear(); var months = end.getUTCMonth() - start.getUTCMonth(); var days = end.getUTCDate() - start.getUTCDate(); if (days < 0) { months -= 1; var prevMonth = new Date(Date.UTC(end.getUTCFullYear(), end.getUTCMonth(), 0)); days += prevMonth.getUTCDate(); } if (months < 0) { years -= 1; months += 12; } return { years: years, months: months, days: days }; } function _dbWeekdayName(d) { var names = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; return names[d.getUTCDay()]; } function _dbFormatLongDate(d) { var months = ['January','February','March','April','May','June','July','August','September','October','November','December']; return months[d.getUTCMonth()] + ' ' + d.getUTCDate() + ', ' + d.getUTCFullYear(); } function _dbCompute() { var modeAge = document.getElementById('db-mode-age') && document.getElementById('db-mode-age').checked; var from = _dbParse(document.getElementById('db-from').value); var to = _dbParse(document.getElementById('db-to').value); var out = document.getElementById('box2'); if (!out) return; if (!from && !to) { out.value = ''; return; } if (!from || !to) { out.value = 'Pick both dates above to see the result.'; return; } var msPerDay = 24 * 60 * 60 * 1000; var earlier = from < to ? from : to; var later = from < to ? to : from; var totalDays = Math.round((later - earlier) / msPerDay); var weeks = Math.floor(totalDays / 7); var leftoverDays = totalDays - weeks * 7; var ymd = _dbDiffYearsMonthsDays(earlier, later); var lines = []; if (modeAge) { lines.push('Age on ' + _dbFormatLongDate(later) + ': ' + ymd.years + ' years, ' + ymd.months + ' months, ' + ymd.days + ' days'); lines.push(''); lines.push('Total days lived: ' + totalDays.toLocaleString()); lines.push('Total weeks lived: ' + weeks.toLocaleString() + ' weeks and ' + leftoverDays + ' day' + (leftoverDays === 1 ? '' : 's')); lines.push('Total months: ' + (ymd.years * 12 + ymd.months).toLocaleString() + ' months and ' + ymd.days + ' day' + (ymd.days === 1 ? '' : 's')); lines.push('Born on ' + _dbWeekdayName(earlier) + ', ' + _dbFormatLongDate(earlier)); } else { lines.push(_dbFormatLongDate(earlier) + ' (' + _dbWeekdayName(earlier) + ') to ' + _dbFormatLongDate(later) + ' (' + _dbWeekdayName(later) + ')'); lines.push(''); lines.push('Total: ' + totalDays.toLocaleString() + ' day' + (totalDays === 1 ? '' : 's')); lines.push('Weeks: ' + weeks.toLocaleString() + ' week' + (weeks === 1 ? '' : 's') + (leftoverDays ? ' and ' + leftoverDays + ' day' + (leftoverDays === 1 ? '' : 's') : '')); lines.push('Calendar: ' + ymd.years + ' year' + (ymd.years === 1 ? '' : 's') + ', ' + ymd.months + ' month' + (ymd.months === 1 ? '' : 's') + ', ' + ymd.days + ' day' + (ymd.days === 1 ? '' : 's')); lines.push('Months only: ' + (ymd.years * 12 + ymd.months).toLocaleString()); } out.value = lines.join('\n'); } function _dbApplyMode() { var modeAge = document.getElementById('db-mode-age') && document.getElementById('db-mode-age').checked; var fromLabel = document.getElementById('db-from-label'); var toLabel = document.getElementById('db-to-label'); if (fromLabel) fromLabel.textContent = modeAge ? 'Birth date' : 'From'; if (toLabel) toLabel.textContent = modeAge ? 'As of' : 'To'; _dbCompute(); } function _dbInit() { var to = document.getElementById('db-to'); if (to && !to.value) to.value = _dbIsoToday(); var radios = document.querySelectorAll('input[name="db-mode"]'); radios.forEach(function(r) { r.addEventListener('change', _dbApplyMode); }); var from = document.getElementById('db-from'); if (from) from.addEventListener('input', _dbCompute); if (to) to.addEventListener('input', _dbCompute); var todayBtn = document.getElementById('db-today'); if (todayBtn) todayBtn.addEventListener('click', function() { var t = document.getElementById('db-to'); if (t) { t.value = _dbIsoToday(); _dbCompute(); } }); _dbApplyMode(); } function convert(input) { return document.getElementById('box2') ? document.getElementById('box2').value : ''; } document.addEventListener('DOMContentLoaded', _dbInit); if (document.readyState !== 'loading') _dbInit(); var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }